/**
 * 定义 SPU 模块相关的接口
 */
import request from '@/utils/request'
import {
  SpuDataResponseData,
  ALLTrademark,
  SpuImageList,
  SpuSaleAttrListResponseData,
  BaseSaleAttrListResponseData,
  SpuData,
  SkuData,
  SkuDataResponseData,
} from '@/api/product/spu/type'

/**
 * 获取 SPU 模块相关的接口地址
 */
enum API {
  // 获取 SPU 列表的接口地址
  SPU_URL = '/admin/product/',
  // 获取全部品牌的数据接口
  ALL_TRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  // 获取 SPU 照片墙的数据接口
  SPU_IMAGE_URL = '/admin/product/spuImageList/',
  // 获取 SPU 销售属性的接口地址
  SPU_SALE_ATTR_URL = '/admin/product/spuSaleAttrList/',
  // 获取全部销售属性的接口地址
  ALL_SALE_ATTR_URL = '/admin/product/baseSaleAttrList',
  // 新增 SPU 的接口地址
  ADD_SPU_URL = '/admin/product/saveSpuInfo',
  // 修改 SPU 的接口地址
  UPDATE_SPU_URL = '/admin/product/updateSpuInfo',
  // 删除 SPU 的接口地址
  DELETE_SPU_URL = '/admin/product/deleteSpu/',
  // 为追加一个新的 SKU 的接口地址
  ADD_SKU_URL = '/admin/product/saveSkuInfo',
  // 查看已有 SPU 的 SKU 数据
  GET_SKU_LIST_URL = '/admin/product/findBySpuId/',
}

/**
 * 获取 SPU 列表
 */
export const reqSpuList = (
  page: number,
  limit: number,
  category3Id: number | string,
) => {
  return request.get<any, SpuDataResponseData>(
    API.SPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )
}

/**
 * 获取全部品牌的数据
 */
export const reqAllTrademark = () => {
  return request.get<any, ALLTrademark>(API.ALL_TRADEMARK_URL)
}

/**
 * 获取 SPU 照片墙的数据
 */
export const reqSpuImageList = (spuId: number | string) => {
  return request.get<any, SpuImageList>(API.SPU_IMAGE_URL + spuId)
}

/**
 * 获取 SPU 销售属性的数据
 */
export const reqSpuSaleAttrList = (spuId: number | string) => {
  return request.get<any, SpuSaleAttrListResponseData>(
    API.SPU_SALE_ATTR_URL + spuId,
  )
}

/**
 * 获取全部销售属性的数据
 */
export const reqAllSaleAttr = () => {
  return request.get<any, BaseSaleAttrListResponseData>(API.ALL_SALE_ATTR_URL)
}

/**
 * 新增 SPU 或 修改 SPU
 */
export const reqAddOrUpdateSpu = (data: SpuData) => {
  if (data.id) {
    return request.post<any, any>(API.UPDATE_SPU_URL, data)
  } else {
    return request.post<any, any>(API.ADD_SPU_URL, data)
  }
}

/**
 * 删除 SPU
 */
export const reqDeleteSpu = (spuId: number | string) => {
  return request.delete<any, any>(API.DELETE_SPU_URL + spuId)
}

/**
 * 追加一个新的 SKU
 */
export const reqAddSku = (data: SkuData) => {
  return request.post<any, any>(API.ADD_SKU_URL, data)
}

/**
 * 查看已有 SPU 的 SKU 数据
 */
export const reqGetSkuList = (spuId: number | string) => {
  return request.get<any, SkuDataResponseData>(API.GET_SKU_LIST_URL + spuId)
}
